package com.ciaojian.core.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ciaojian.core.enums.business.WorkerApplyStatusEnum;
import com.ciaojian.core.enums.business.WorkerApplyTypeEnum;
import com.ciaojian.core.mapper.WorkerApplyMapper;
import com.ciaojian.core.model.WorkerApply;

import java.util.Date;

/**
 * <p>
 * 员工申请表 服务类
 * </p>
 *
 * @author Atlakyn
 * @since 2021-06-09
 */
public abstract class WorkerApplyService<M extends BaseMapper<T>, T> extends ServiceImpl<WorkerApplyMapper, WorkerApply> {

    /**
     * 统计本月申请次数
     *
     * @param workerId 发型师 ID
     * @param type     申请类型
     * @return 次数
     */
    public Integer countThisMonth(Integer workerId, WorkerApplyTypeEnum type) {
        Date currentDate = new Date();
        return lambdaQuery()
                .eq(WorkerApply::getWorkerId, workerId)
                .eq(WorkerApply::getType, type)
                .between(WorkerApply::getApplyDate, DateUtil.beginOfMonth(currentDate), DateUtil.endOfMonth(currentDate))
                .in(WorkerApply::getStatus, WorkerApplyStatusEnum.PASS, WorkerApplyStatusEnum.WAIT)
                .count();
    }

    /**
     * 获取发型师待审核申请
     *
     * @param workerId 发型师ID
     * @return 待审核申请
     */
    public WorkerApply getWaitByWorker(Integer workerId) {
        return this.lambdaQuery()
                .eq(WorkerApply::getStatus, WorkerApplyStatusEnum.WAIT)
                .eq(WorkerApply::getWorkerId, workerId)
                .one();
    }
}
